﻿using Engine;
using Game.NetWork;

namespace Game
{
    public class FCBlockEditPackage : IPackage
    {
        public enum EventType
        {
            OpenInventoryByID,
            OpenInventoryByPoint,
            CrossbowPull,
            EditSign
        }

        public int m_inventoryId;

        public int m_slotIndex;

        public EventType m_type;

        public Point3 m_point3;

        public byte ID => 101;

        public Client To { get; set; }

        public Client Except { get; set; }

        public Client From { get; set; }

        public ClientState MinNeedState => ClientState.ProjectLoaded;

        public FCBlockEditPackage()
        {
        }

        public FCBlockEditPackage(IInventory inventory)
        {
            m_inventoryId = inventory.Id;
            m_type = EventType.OpenInventoryByID;
        }

        public FCBlockEditPackage(Point3 point3, EventType type)
        {
            m_point3 = point3;
            m_type = type;
        }

        public FCBlockEditPackage(IInventory inventory, int slotIndex, EventType type)
        {
            m_inventoryId = inventory.Id;
            m_slotIndex = slotIndex;
            m_type = type;
        }

        public void WriteData(PackageStreamWriter writer)
        {
            writer.WriteEnum(m_type);
            switch (m_type)
            {
                case EventType.OpenInventoryByID:
                    writer.Write(m_inventoryId);
                    break;
                case EventType.OpenInventoryByPoint:
                    writer.Write(m_point3.X);
                    writer.Write(m_point3.Y);
                    writer.Write(m_point3.Z);
                    break;
            }
        }

        public void ReadData(PackageStreamReader reader)
        {
            m_type = reader.ReadEnum<EventType>();
            switch (m_type)
            {
                case EventType.OpenInventoryByID:
                    m_inventoryId = reader.ReadInt32();
                    break;
                case EventType.OpenInventoryByPoint:
                    int x = reader.ReadInt32();
                    int y = reader.ReadInt32();
                    int z = reader.ReadInt32();
                    m_point3 = new Point3(x, y, z);
                    break;
            }
        }


        public void Handle(ProjectNet projectNet, NetNode netNode, bool isServer)
        {
            if (From == null)
            {
                Log.Information("出现空玩家打开背包");
                return;
            }

            SubsystemInventories subsystemInventories = projectNet.FindSubsystem<SubsystemInventories>();
            switch (m_type)
            {
                case EventType.OpenInventoryByID:
                    {
                        if (isServer)
                        {
                            IInventory inventoryById = subsystemInventories.GetInventoryById(m_inventoryId);
                            if (inventoryById != null)
                            {
                                IPackage package2 = new FCBlockEditPackage(inventoryById);
                                package2.To = From;
                                CommonLib.Net.QueuePackage(package2);
                            }

                            break;
                        }

                        IInventory inventoryById2 = subsystemInventories.GetInventoryById(m_inventoryId);
                        if (inventoryById2 != null)
                        {
                            ComponentPlayer mainPlayer = CommonLib.MainPlayer;
                            if (inventoryById2 is FCComponentGaolu componentGaolu)
                            {
                                mainPlayer.ComponentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new GaoluWidget(mainPlayer.ComponentMiner.Inventory, componentGaolu);
                                AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
                            }
                            else if (inventoryById2 is ComponentFlameFurnace componentFlameFurnace)
                            {
                                mainPlayer.ComponentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new FlameFurnaceWidget(mainPlayer.ComponentMiner.Inventory, componentFlameFurnace);
                                AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
                            }
                            else if (inventoryById2 is FCComponentFajiaotong componentFajiaotong)
                            {
                                mainPlayer.ComponentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new FajiaotongWidget(mainPlayer.ComponentMiner.Inventory, componentFajiaotong);
                                AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
                            }
                            else if (inventoryById2 is ComponentXCraftingTable componentCraftingTable)
                            {
                                mainPlayer.ComponentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new XCraftingTableWidget(mainPlayer.ComponentMiner.Inventory, componentCraftingTable, mainPlayer.ComponentMiner.ComponentPlayer);
                                AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
                            }
                            else if (inventoryById2 is FCComponentMicro componentMicro)
                            {
                                mainPlayer.ComponentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new MicroWidget(mainPlayer.ComponentMiner.Inventory, componentMicro);
                                AudioManager.PlaySound("Audio/UI/start", 1f, 0f, 0f);
                            }
                            else if (inventoryById2 is FCComponentFermentation componentDissolveStove)
                            {
                                mainPlayer.ComponentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new FermentationWidget(mainPlayer.ComponentMiner.Inventory, componentDissolveStove);
                                AudioManager.PlaySound("Audio/UI/start", 1f, 0f, 0f);
                            }
                            else if (inventoryById2 is ComponentCoffer componentChest)
                            {
                                mainPlayer.ComponentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new CofferWidget(mainPlayer.ComponentMiner.Inventory, componentChest);
                                AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
                            }
                            else if (inventoryById2 is ComponentXChest componentChest2)
                            {
                                mainPlayer.ComponentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new ChestWidget(mainPlayer.ComponentMiner.Inventory, componentChest2);
                                AudioManager.PlaySound("Audio/UI/start", 1f, 0f, 0f);
                            }
                        }

                        break;
                    }
                case EventType.OpenInventoryByPoint:
                    {
                        if (!isServer)
                        {
                            break;
                        }

                        ComponentBlockEntity blockEntity = projectNet.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(m_point3.X, m_point3.Y, m_point3.Z);
                        if (blockEntity != null)
                        {
                            IInventory inventory = blockEntity.Entity.FindComponent<IInventory>(throwOnError: false);
                            if (inventory != null)
                            {
                                IPackage package = new FCBlockEditPackage(inventory);
                                package.To = From;
                                CommonLib.Net.QueuePackage(package);
                            }
                        }

                        break;
                    }
            }
        }
    }
}